import 'package:android_intent_plus/android_intent.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../AppConfig/Config.dart';
import 'package:platform/platform.dart';

import '../../AppConfig/ShowDialog.dart';
class CostNegotiation extends StatefulWidget {
  const CostNegotiation({super.key});

  @override
  State<CostNegotiation> createState() => _CostNegotiationState();
}

class _CostNegotiationState extends State<CostNegotiation> {
  List typeList=["货物损失","迟到/爽约","加价",];
  List chooseList=[];
  _makePhoneCall(String phoneNumber) async {
    if (LocalPlatform().isAndroid) {
      var status = await Permission.phone.status;
      if (status.isGranted) {
        // 权限已被授予
        // callPhone();
        final AndroidIntent intent = AndroidIntent(
          action: 'android.intent.action.CALL',
          data: Uri.encodeFull('tel:$phoneNumber'),
        );
        await intent.launch();
      } else if (status.isDenied) {
        showCallPhoneFirstDialog(context);
        // 权限被拒绝，但用户可以再次授权
        status = await Permission.phone.request();
        if (status.isGranted) {
          // 用户同意了权限
          // callPhone();
          final AndroidIntent intent = AndroidIntent(
            action: 'android.intent.action.CALL',
            data: Uri.encodeFull('tel:$phoneNumber'),
          );
          await intent.launch();
        } else {
          showCallPhoneDialog(context);
          // 用户拒绝权限，无法发起电话
          // 可以在此处展示信息提示用户需要权限
        }
      } else if (status.isPermanentlyDenied) {
        // 权限被永久拒绝，需要用户手动开启权限
        showCallPhoneDialog(context);
      }

    }else{
      final url =Uri.parse('tel:$phoneNumber');
      if (await canLaunchUrl(url)) {
        await launchUrl(url);
      } else {
        throw '无法拨打电话：$url';
      }
    }
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        backgroundColor:Colors.white,
        centerTitle: true,
        title: Text("费用协商",style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 16.sp),),
        actions: [
          TextButton(onPressed: (){
            _makePhoneCall('1234567890');
          }, child: Text("联系司机",style: TextStyle(color: Colors.black),)),
        ],
      ),
      body: Column(
        children: [
          Expanded(
              child: ListView(
          children: [
              Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10)),
                ),
                margin: EdgeInsets.only(top: 8.h,left: 10.w,right: 10.w),
                padding: EdgeInsets.only(top: 11.h,bottom: 11.h),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: 4.w,
                      height: 16.h,
                      color: MyColors.themeColor,
                    ),
                    SizedBox(width: 13.w,),
                    Column(
                      children: [
                        Image.network( "https://t12.baidu.com/it/u=2009860844,206760771&fm=30&app=106&f=JPEG?w=640&h=426&s=41313C722D374B925CF3F3D30300D0A9",
                          width: 36.w,
                          height: 36.h,
                          fit: BoxFit.cover,
                        ),
                        SizedBox(height: 6.h,),
                        Text("司机",style: TextStyle(color: Colors.black),),
                      ],
                    ),
                    SizedBox(width: 11.w,),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text("苏州 吴中区",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w600),),
                            SizedBox(width: 5.w,),
                            Image.asset("lib/Images/PersonCenter/我的评价箭头.png",width: 30.w,height: 30.h,),
                            SizedBox(width: 5.w,),
                            Text("苏州 吴中区",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w600),),
                          ],
                        ),

                        Text("运单编号：11112222333344445",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                        Text("运单时间：2023-05-05 10:29:59",style: TextStyle(color: MyColors.textColor,fontSize: 13.sp),),
                      ],
                    ),
                  ],
                ),
              ),
            Container(
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              margin: EdgeInsets.only(top: 8.h,left: 10.w,right: 10.w),
              padding: EdgeInsets.only(top: 11.h,bottom:20.h),
              child:Column(
                children: [
                  Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: 4.w,
                          height: 16.h,
                          color: MyColors.themeColor,
                        ),
                        SizedBox(width: 13.w,),
                        Text("请选择费用类型",style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                      ]
                  ),
                  Container(margin: EdgeInsets.only(top: 10.h,bottom: 11.h,),
                    height: 5.h,width: screenSize.width-20.w,color: MyColors.backgreyColor,),
                  SizedBox(height: 10.h,),
                  Padding(
                    padding: const EdgeInsets.all(0),
                    child: Wrap(
                      spacing: 8.w, // 两个Container之间的水平间距
                      runSpacing: 11.h, // 行与行之间的垂直间距
                      children: typeList.map((length) {
                        bool? isSelected = chooseList.contains(length);

                        return GestureDetector(
                          onTap: () {
                            // 处理点击事件
                            setState(() {
                              if (isSelected) {
                                //如果已经被选中，则移除
                                chooseList.remove(length);

                              } else  {
                                // 如果未被选中且未达到最大选择数量，则添加
                                chooseList.add(length);

                              }
                            });
                          },
                          child: Container(
                            width: 100.w, // Container宽度
                            height: 34.0, // Container高度，使其成为正方形
                            padding: EdgeInsets.only(left: 8.w,right: 8.w),
                            decoration: BoxDecoration(
                              border: Border.all(
                                color: isSelected ?MyColors.themeColor:MyColors.backgreyColor , // 边框颜色
                                width: 1.0, // 边框宽度
                              ),
                              color: isSelected ? Color.fromRGBO(251, 236, 228, 1) : MyColors.backgreyColor, // 根据状态决定颜色
                              borderRadius: BorderRadius.circular(3.0), // 设置Container的圆角
                            ),
                            child: Center(
                              child: Text(
                                length,
                                style: TextStyle(
                                  fontSize: 13.sp,
                                  color: isSelected ?MyColors.themeColor:Colors.black, // 文字颜色为白色
                                ),
                              ),
                            ),
                          ),
                        );
                      }).toList(),
                    ),
                  ),
                ],
              ),

            ),
            Container(
              margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h),
              padding: EdgeInsets.only(top: 14.h,right: 10.w,bottom: 19.h),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Row(
                        children: [
                          Container(
                            width: 5.w,
                            height: 16.h,
                            color: MyColors.themeColor,
                          ),
                          SizedBox(width: 13.w,),
                          Text("申请金额",style: TextStyle(color: Colors.black,fontSize: 16.sp),),

                        ],
                      ),
                      Row(
                        children: [
                          Container(
                            width: 150.w,
                            height: 20.h,
                            color: MyColors.backgreyColor,
                            child:  TextField(
                              decoration: InputDecoration(
                                hintText: '必填，请填写准确数字',
                                hintStyle:TextStyle(fontSize: 13.sp,color: MyColors.hintColor),
                                filled: true,
                                fillColor: Colors.white,
                                border: InputBorder.none,
                                alignLabelWithHint: true,
                                contentPadding: EdgeInsets.symmetric(vertical: 6.0.h, horizontal: 0.0.w),
                              ),
                              keyboardType: TextInputType.number,
                              textAlign: TextAlign.right,
                              style: TextStyle(color: Colors.black,fontSize: 13.sp),
                              onChanged: (value) {
                                setState(() {

                                });
                              },
                            ),
                          ),
                          Text("元",style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                          Icon(Icons.border_color_outlined,color: MyColors.authTipColor,),
                        ],
                      ),
                    ],
                  ),
                  SizedBox(height: 9.h,),
                  Center(
                    child: Container(
                      color:Color.fromRGBO(251, 241, 233, 1) ,
                      padding: EdgeInsets.only(left: 9.w),
                      height: 26.h,
                      width: screenSize.width-55.w,
                      child: Row(
                        children: [
                        Icon(Icons.volume_up_outlined,color: MyColors.themeColor,),
                          Text("请填写准确数字，有助于货主更好处理费用问题",style: TextStyle(color: MyColors.themeColor,fontSize: 12.sp),),
                        ],
                      ),
                    ),
                  ),

                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h),
              padding: EdgeInsets.only(left: 14.w,top: 9.h,right: 18.w,bottom: 19.h),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text("申请原因",style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                  SizedBox(height: 9.h,),
                  Container(
                    width: screenSize.width-56.w,
                    child: BrnInputText(
                      maxHeight: 200.h,
                      minHeight: 60.h,
                      minLines: 1,
                      maxLength: 140,
                      bgColor:MyColors.backgreyColor,
                      textString:  '',
                      textInputAction: TextInputAction.newline,
                      maxHintLines: 20,
                      hint: '必填，请输入申请原因',
                      padding: EdgeInsets.fromLTRB(20, 10, 20, 14),
                      onTextChange: (text) {
                        print(text);
                        setState(() {});
                      },
                      onSubmit: (text) {
                        print(text);
                      },
                    ),
                  )
                ],
              ),
            ),
        ],
      ),
          ),
          Container(
            height: 85.h,
            color: Colors.white,
            width: screenSize.width,
            padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 10.h,bottom: 40.h),
            child: BrnSmallMainButton(
              bgColor: MyColors.themeColor,
              textColor: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              radius: 30,
              title: '提交申请',
              onTap: () {
                BrnToast.show('录需求信息', context);
              },
            ),
          ),
        ],
      ),
    );
  }
}
